﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using BawangMemo.BawangMemoView.View;
using System.Windows.Controls;

namespace BawangMemo.BawangMemoView
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            //Bawang_Setting setting = new Bawang_Setting();
            //setting.comboBox1.SelectedIndex = Bawang_Setting.skinIndex;
            //Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            //{
            //    Source = new Uri("Skin/" +
            //        ((ComboBoxItem)setting.comboBox1.SelectedItem).Content + ".xaml",
            //        UriKind.Relative)
            //};
        }
        public static bool IsShuttingDown { get; set; }
        
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            IsShuttingDown = true;

            Bawang_Setting setting = new Bawang_Setting();
            //setting.comboBox1.SelectedIndex = Bawang_Setting.skinIndex;
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            {
                Source = new Uri("Skin/" +
                    ((ComboBoxItem)setting.comboBox1.Items[Bawang_Setting.skinIndex]).Content +
                    ".xaml", UriKind.Relative)
            };
            GC.Collect();
        }

        #region Time_Changing
        bool ac_bool = true;

        private void Application_Deactivated(object sender, EventArgs e)
        {
            //MainFrame_MiniMizedToTuoPan();
            ment_time[0] = BawangMemoView.View.Bawang_Setting._minute * 60;
            ac_bool = false;

            Timer_Begin();
        }

        private void Application_Activated(object sender, EventArgs e)
        {
            if (ac_bool == false)
            {
                ac_bool = true;
                Timer_close();
            }
        }




        //System.Windows.Forms.NotifyIcon notifyIcon;
        System.Timers.Timer t;
        //Boolean onscreen_flag; //表示是否显示出来
        int[] ment_time = new int[] { 900, 8, 20 };//每过多少秒提醒一次

        private void time_remind(object source, System.Timers.ElapsedEventArgs e)
        {
            System.Timers.Timer tim = (System.Timers.Timer)source;
            //如果已经点开了，那么就不用下一个提醒了
            //if ((onscreen_flag == false) || 
            if ((ac_bool == false))
            {
                MessageBox.Show("别玩了，继续背单词吧！");
                //这个字符串就是提醒的语句
                if ((tim.Interval < ment_time[ment_time.Length - 1] * 1000))
                {
                    int j = 0;
                    for (int i = 0; i < ment_time.Length; ++i)
                        if (ment_time[i] * 1000 == tim.Interval)
                        {
                            j = i; break;
                        }
                    //找到下一个提醒时刻点
                    t = new System.Timers.Timer();
                    // 1000=1 second
                    t.Interval = (ment_time[j + 1] - ment_time[j]) * 1000;
                    t.Enabled = true;
                    t.AutoReset = false;
                    t.Elapsed += new System.Timers.ElapsedEventHandler(time_remind);
                }
            }
            tim.Stop();
            tim.Dispose();
            //把这个timer释放掉
        }
        private void Timer_close()
        {
            t.Stop();
            t.Dispose();
        }
        private void Timer_Begin()
        {
            t = new System.Timers.Timer();
            // 1000=1 second
            if (ment_time[0] * 1000 < 0) t.Interval = 99999999; else t.Interval = ment_time[0] * 1000;
            t.Enabled = true;
            t.AutoReset = false; //只提醒一次
            t.Elapsed += new System.Timers.ElapsedEventHandler(time_remind);
            //            t.Start();
        }

        #endregion
    }
}
